package com.fzj.example.io;

import java.io.*;

/**
 * ClassName: CopyFileDemo
 * date: 2019/11/8 9:21
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class CopyFileDemo {

    public static void main(String[] args) throws IOException {
        /*****************************方式1*************************************/

        //输入流，读取文件内容，然后将内容，不断的发给输出流
        FileInputStream fileInputStream = new FileInputStream("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io.pdf");
        BufferedInputStream inBuffer = new BufferedInputStream(fileInputStream);
        File file=new File("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io_copy.pdf");
        if(!file.exists()){
            file.getParentFile().mkdir();
            file.createNewFile();
        }
        FileOutputStream fileOutputStream=new FileOutputStream(file);
        BufferedOutputStream outBuffer=new BufferedOutputStream(fileOutputStream);
        int len=0;
        //一次性最多度1024
        byte[] bs=new byte[1024];
        long startTime=System.currentTimeMillis();
        while ((len=inBuffer.read(bs))!=-1){
            outBuffer.write(bs,0,len);
        }
        long endTime=System.currentTimeMillis();
        System.out.println("缓冲读写消耗时常："+(endTime-startTime)+"ms");
        outBuffer.close();
        fileInputStream.close();
        inBuffer.close();
        fileInputStream.close();
        /*****************************方式2*************************************/
        //输入流，读取文件内容，然后将内容，不断的发给输出流
        FileInputStream fileInputStream2 = new FileInputStream("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io.pdf");
        BufferedInputStream inBuffer2 = new BufferedInputStream(fileInputStream2);
        File file2=new File("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io_copy2.pdf");
        if(!file2.exists()){
            file2.getParentFile().mkdir();
            file2.createNewFile();
        }
        FileOutputStream fileOutputStream2=new FileOutputStream(file2);
        int len2=0;
        //一次性最多度1024
        byte[] bs2=new byte[1024];
        long startTime2=System.currentTimeMillis();
        while ((len2=inBuffer2.read(bs))!=-1){
            fileOutputStream2.write(bs2,0,len2);
        }
        long endTime2=System.currentTimeMillis();
        System.out.println("缓冲读，节点流写消耗时常："+(endTime2-startTime2)+"ms");
        fileInputStream2.close();
        inBuffer2.close();
        fileInputStream2.close();
        /*****************************方式3*************************************/
        FileInputStream fileInputStream3 = new FileInputStream("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io.pdf");
        File file3=new File("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io_copy3.pdf");
        if(!file3.exists()){
            file3.getParentFile().mkdir();
            file3.createNewFile();
        }
        FileOutputStream fileOutputStream3=new FileOutputStream(file3);
        int len3=0;
        //一次性最多度1024
        byte[] bs3=new byte[1034];
        long startTime3=System.currentTimeMillis();
        while ((len3=fileInputStream3.read(bs))!=-1){
            fileOutputStream3.write(bs3,0,len3);
        }
        long endTime3=System.currentTimeMillis();
        System.out.println("节点流读，节点流写消耗时常一次性1024个："+(endTime3-startTime3)+"ms");
        fileInputStream3.close();
        fileInputStream3.close();
        /*****************************方式4*************************************/
        FileInputStream fileInputStream4 = new FileInputStream("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io.pdf");
        File file4=new File("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\io_copy4.pdf");
        if(!file4.exists()){
            file4.getParentFile().mkdir();
            file4.createNewFile();
        }
        FileOutputStream fileOutputStream4=new FileOutputStream(file4);
        int charint=0;
        //一次性最多度1024
        byte[] bs4=new byte[1044];
        long startTime4=System.currentTimeMillis();
        while ((charint=fileInputStream4.read())!=-1){
            fileOutputStream4.write(charint);
        }
        long endTime4=System.currentTimeMillis();
        System.out.println("节点流读，节点流写消耗时常每一次一个字节："+(endTime4-startTime4)+"ms");
        fileInputStream4.close();
        fileInputStream4.close();
    }
}
